package com.ruanyun.sys.modular.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruanyun.base.pojo.page.LayuiPageFactory;
import com.ruanyun.base.pojo.page.LayuiPageInfo;
import com.ruanyun.sys.modular.system.entity.Stations;
import com.ruanyun.sys.modular.system.entity.UserSta;
import com.ruanyun.sys.modular.system.mapper.StationsMapper;
import com.ruanyun.sys.modular.system.model.params.StationsParam;
import com.ruanyun.sys.modular.system.model.result.StationsResult;
import  com.ruanyun.sys.modular.system.service.StationsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruanyun.sys.modular.system.service.UserStaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位 服务实现类
 * </p>
 *
 * @author wangchuan
 * @since 2019-08-15
 */
@Service
public class StationsServiceImpl extends ServiceImpl<StationsMapper, Stations> implements StationsService {

    @Autowired
    private UserStaService userStaService;

    @Override
    public void add(StationsParam param){
        Stations entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StationsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StationsParam param){
        Stations oldEntity = getOldEntity(param);
        Stations newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StationsResult findBySpec(StationsParam param){
        return null;
    }

    @Override
    public List<StationsResult> findListBySpec(StationsParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StationsParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(StationsParam param){
        return param.getStationsId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Stations getOldEntity(StationsParam param) {
        return this.getById(getKey(param));
    }

    private Stations getEntity(StationsParam param) {
        Stations entity = new Stations();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 获取多选框的岗位列表
     *
     * @author administrator
     * @Date 2019-06-27
     */
    @Override
    public LayuiPageInfo listStations(Long userId){
        //找出所有职位
        List<Map<String, Object>> list = this.baseMapper.getAllStationMap();

        //用户id为空，则直接返回
        if (userId == null) {

        } else {

            //查询用户的职位id列表
            List<UserSta> userPosList = this.userStaService.list(new QueryWrapper<UserSta>().eq("user_id", userId));

            if (userPosList != null && userPosList.size() > 0) {
                for (UserSta userSta : userPosList) {
                    for (Map<String, Object> positionMap : list) {
                        if (userSta.getStaId().equals(positionMap.get("stationsId"))) {
                            positionMap.put("selected", true);
                        }
                    }
                }
            }
        }

        LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
        layuiPageInfo.setData(list);
        return layuiPageInfo;
    }

}
